local ex__benghuai = fk.CreateSkill {

  name = "joyex__benghuai",

  tags = { Skill.Compulsory, },

}



ex__benghuai:addEffect(fk.TurnStart, {
  name = "joyex__benghuai",
  anim_type = "negative",
  frequency = Skill.Compulsory,
  events = {fk.TurnStart,fk.EventPhaseStart},
  can_trigger = function(self, event, target, player, data)
    if event == fk.TurnStart then
      return target == player and player:hasSkill(ex__benghuai.name) and not table.find(player.room.alive_players, function (p)
        return p.hp < player.hp
      end)
    elseif event == fk.EventPhaseStart then
      return target.phase == Player.Finish and target:hasSkill(ex__benghuai.name) and  target == player  and  table.find(player.room.alive_players, function (p)
        return p.hp < player.hp
      end)
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.TurnStart then
      room:setPlayerMark(player,"@@joy__roulin-turn",1)
    else
      room:doIndicate(target.id,{player.id})
      local choice = room:askForChoice(player, {"loseMaxHp", "loseHp"}, ex__benghuai.name)
      if choice == "loseMaxHp" then
        room:changeMaxHp(player, -1)
      else
        room:loseHp(player, 1, ex__benghuai.name)
      end
      if not player.dead then
        player:drawCards(1, ex__benghuai.name)
      end
    end
  end,
})
ex__benghuai:addEffect(fk.EventPhaseStart, {
  name = "joyex__benghuai",
  anim_type = "negative",
  frequency = Skill.Compulsory,
  events = {fk.TurnStart,fk.EventPhaseStart},
  can_trigger = function(self, event, target, player, data)
    if event == fk.TurnStart then
      return target == player and player:hasSkill(ex__benghuai.name) and not table.find(player.room.alive_players, function (p)
        return p.hp < player.hp
      end)
    elseif event == fk.EventPhaseStart then
      return target.phase == Player.Finish and target:hasSkill(ex__benghuai.name) and  target == player  and  table.find(player.room.alive_players, function (p)
        return p.hp < player.hp
      end)
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.TurnStart then
      room:setPlayerMark(player,"@@joy__roulin-turn",1)
    else
      room:doIndicate(target.id,{player.id})
      local choice = room:askForChoice(player, {"loseMaxHp", "loseHp"}, ex__benghuai.name)
      if choice == "loseMaxHp" then
        room:changeMaxHp(player, -1)
      else
        room:loseHp(player, 1, ex__benghuai.name)
      end
      if not player.dead then
        player:drawCards(1, ex__benghuai.name)
      end
    end
  end,
})

return ex__benghuai